// pages/fwzsMine/fwzsMine.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    isHalf: true,
    bottabData: app.globalData.bottabDataForFwzs,
    houseFlag: '',
    currentPage: 1,
    uesrId: '',
    houseList: [],
    hasData: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('userInfo')){
      this.setData({
        uesrId: JSON.parse(wx.getStorageSync('userInfo')).id
      })
    }
    this.setData({
      ['bottabData[1].current']: true,
      houseFlag: options.flag
    })
  },
  switchBotab(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url
    })
  },
  // 查看我的收藏
  getMyCollect: function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyHouseRental/collection/byUserId',
      data: {
        userId: _this.data.uesrId,
        currentPage: _this.data.currentPage,
        pageNumber: '10',
      },
      success: function (res) {
        if (res.data.success) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          var houses = res.data.data.pp;
          for (var i = 0; i < houses.length; i++) {
            if (houses[i].picture) {
              houses[i].picture = houses[i].picture.split(",");
            }
            if (houses[i].housingCharacteristics) {
              houses[i].housingCharacteristics = houses[i].housingCharacteristics.split(",");
            }
            if (houses[i].housingDescription && houses[i].housingDescription.length > 70){
              houses[i].housingDescription = houses[i].housingDescription.substring(0,70) + '...'
            }
            _this.data.houseList.push(houses[i])
          }
          _this.setData({
            houseList: _this.data.houseList
          })
          if (_this.data.houseList.length == res.data.data.page.totalNumber) {
            _this.setData({
              hasData: false
            })
          }
        }
      }
    })
  },
  // 查看我的发布
  getMyPublish: function () {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyHouseRental/release/byUserId',
      data: {
        userId: _this.data.uesrId,
        currentPage: _this.data.currentPage,
        pageNumber: '10'
      },
      success: function (res) {
        if (res.data.success) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          var houses = res.data.data.pp;
          for (var i = 0; i < houses.length; i++) {
            if (houses[i].picture) {
              houses[i].picture = houses[i].picture.split(",");
            }
            if (houses[i].housingCharacteristics) {
              houses[i].housingCharacteristics = houses[i].housingCharacteristics.split(",");
            }
            if (houses[i].housingDescription && houses[i].housingDescription.length > 70) {
              houses[i].housingDescription = houses[i].housingDescription.substring(0, 70) + '...'
            }
            _this.data.houseList.push(houses[i])
          }
          _this.setData({
            houseList: _this.data.houseList
          })
          if (_this.data.houseList.length == res.data.data.page.totalNumber) {
            _this.setData({
              hasData: false
            })
          }
        }
      }
    })
  },
  // 进入详情页
  enterDetail: function (e) {
    var flag = e.currentTarget.dataset.flag;
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/houseDetail/houseDetail?flag=' + flag + '&name=' + item.title + '&id=' + item.id + '&communityName=' + item.communityName
    })
  },
  // 改变房源类型
  changeType: function () {
    this.setData({
      ['currentPage']: 1,
      houseList: [],
      hasData: true,
      isHalf: !this.data.isHalf
    })
    if (this.data.isHalf) {
      this.getMyCollect();
    }
    else {
      this.getMyPublish();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.isHalf) {
      this.setData({
        ['currentPage']: 1,
        houseList: []
      })
      this.getMyCollect()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      ['currentPage']: 1,
      houseList: []
    })
    if (this.data.isHalf){
      this.getMyCollect();
    }
    else{
      this.getMyPublish();
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasData) {
      this.setData({
        ['currentPage']: this.data.currentPage + 1
      })
      if (this.data.isHalf) {
        this.getMyCollect();
      }
      else {
        this.getMyPublish();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})